package com.weipeep.sys.controller;


import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weipeep.sys.domain.SchoolDO;
import com.weipeep.sys.service.SchoolService;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 校区
 * </pre>
 * <small> 2018-08-06 14:51:20 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/school")
public class SchoolController {
	@Autowired
	private SchoolService schoolService;
	
	@GetMapping()
	@RequiresPermissions("sys:school:school")
	String School(){
	    return "sys/school/school";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:school:school")
	public Result<IPage<SchoolDO>> list(Integer pageNumber, Integer pageSize, SchoolDO schoolDTO){
		// 查询列表数据
        IPage<SchoolDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<SchoolDO> wrapper = new QueryWrapper<>(schoolDTO);
        page = schoolService.page(page, wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("sys:school:add")
	String add(){
	    return "sys/school/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sys:school:edit")
	String edit(@PathVariable("id") Long id,Model model){
		SchoolDO school = schoolService.getById(id);
		model.addAttribute("school", school);
	    return "sys/school/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:school:add")
	public Result<String> save( SchoolDO school){
		if(schoolService.save(school)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:school:edit")
	public Result<String>  update( SchoolDO school){
		schoolService.updateById(school);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sys:school:remove")
	public Result<String>  remove( Long id){
		if(schoolService.removeById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:school:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		schoolService.removeByIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
